﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using Zzj.Windows;
using SpeechLib;
using Zzj.Model;
using Zzj.Utils;

namespace Zzj.Pages
{
    /// <summary>
    /// Index.xaml 的交互逻辑
    /// </summary>
    public partial class Index : Page
    {
        private CardInfo objIDCardInfo = null;
        private Page currentPage = null;
        private MainWindow mainWindow = null;
        private SpVoice speech = new SpVoice();
        private string[] CardInfo = new string[30];

        //IntPtr icdev = IntPtr.Zero;
        //int st;
        //int sysLanguage = 0;
        private IntPtr icPtr = IntPtr.Zero;

        public Index(MainWindow _win)
        {
            InitializeComponent();

            currentPage = this;

            mainWindow = _win;

            this.Loaded += Page_loaded;
        }

        /// <summary>
        /// 信息提示
        /// </summary>
        /// <param name="message"> </param>
        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void Page_loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// 打印磅单按钮
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e">      </param>
        private void doPrint(object sender, RoutedEventArgs e)
        {
            String facId = "";
            facId = IdCardRead.ReadCardInfo();

            if ("".Equals(facId))
            {
                showMessage("未读取到身份信息，请重新放置身份证");
            }
            else
            {
                goBillPrint(facId);
            }
        }

        /// <summary>
        /// 获取对应身份证单据信息
        /// </summary>
        /// <param name="facId"> </param>
        private void goBillPrint(String facId)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("facId", facId);
            paramJSON.Add("operation", "clientPrint");
            paramJSON.Add("printDuration", App.getConfig().PrintDuration);
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillsByCardForPrint.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "请到人工窗口打印";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            JArray resultArray = httpPostWindow.getResultArray();
            List<BillModel> bills = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            if (null == bills || bills.Count == 0)
            {
                showMessage("未查询到单据");
                return;
            }
            PrintPage printPage = new PrintPage(bills, mainWindow);
            NavigationService.GetNavigationService(this).Navigate(printPage);
        }

        /// <summary>
        /// 报道按钮
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e">      </param>
        private void doCard(object sender, RoutedEventArgs e)
        {
            String facId = "";
            facId = IdCardRead.ReadCardInfo();

            if ("".Equals(facId))
            {
                showMessage("未读取到身份信息，请重新放置身份证");
            }
            else
            {
                goMakeCard(facId);
            }
        }

        /// <summary>
        /// 身份证查询单据
        /// </summary>
        /// <param name="cardCode"> </param>
        private void goMakeCard(String cardCode)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("driverCard", cardCode);
            paramJSON.Add("operation", "forMakeCard");
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillByCard.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "未查询到单据";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            mainWindow.Speak("请仔细核对计划业务类型及计划信息，确认无误后点击对应业务报到按钮，若无对应业务请返回首页");
            JArray resultArray = httpPostWindow.getResultArray();
            List<BillModel> billModel = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            SaleCards card = new SaleCards(billModel, mainWindow);

            NavigationService.GetNavigationService(this).Navigate(card);
        }
    }
}